package com.nitty.cash.loop.ui.activity

import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.lib.common.permission.CommonPermissions
import com.lib.common.permission.Permission
import com.nitty.cash.loop.common.CommonActivity
import com.nitty.cash.loop.databinding.UiGuideBinding
import com.nitty.cash.loop.event.NittyEventPointType
import com.nitty.cash.loop.ui.factory.GuideFactory
import com.nitty.cash.loop.utils.helper.NittyAnalysisHelper
import com.nitty.cash.loop.utils.helper.NittyConfigsHelper
import com.nitty.cash.loop.utils.shared.NittySharedHelper
import com.nitty.cash.loop.utils.shared.NittySharedKey

/**
 * NittyCash
 * @Author wwdeng
 * @CreateTime: 2024/12/9 14:48
 * @Annotation：引导页
 */
class GuideAct: CommonActivity() {
    lateinit var binding: UiGuideBinding
    override fun getNittyRootView(): View? {
        binding = UiGuideBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initNittyView() {
    }

    override fun initNittyListener() {
        binding.guideViewPage.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
            }
        })
    }

    override fun loadNittyData() {
        val pageAdapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int = 3
            override fun createFragment(position: Int): Fragment =
                GuideFactory.create(position)
        }
        binding.guideViewPage.adapter = pageAdapter
    }

    /**
     * 下一页
     */
    fun showGuideNextPage(pageIndex: Int){
        if (pageIndex == 3){
            //下一步
            onGuideStartNextStep()
            return
        }
        binding.guideViewPage.currentItem = pageIndex
    }

    /**
     * 下一步
     */
    private fun onGuideStartNextStep(){
        NittySharedHelper.putNittySharedData(NittySharedKey.NITTY_SHOW_GUIDE_PAGE,true)

        //是否进入过权限页面 注：此字段只在同意 READ_PHONE_STATE 权限之后改为 true
        val isShowLaunchPermission = NittySharedHelper.getNittySharedBoolean(NittySharedKey.NITTY_SHOW_PERMISSION_LAUNCH_PAGE)
        if (!isShowLaunchPermission){
            //进入权限页
            showLaunchPermissionPage()
            return
        }

        // TODO: PhoneState为必须申请的权限
        if (NittyConfigsHelper.getNittyPermissionsLaunch().contains(Permission.READ_PHONE_STATE) && !CommonPermissions.isGranted(this, Permission.READ_PHONE_STATE)){
            //进入权限页
            showLaunchPermissionPage()
            return
        }
        //已登录 - 进入主页
        if (NittySharedHelper.getNittySharedBoolean(NittySharedKey.NITTY_LOGIN)){
            //初始化analysis lib 赋值 用户id 和 token
            NittyAnalysisHelper.initAnalysisLibAtLoginSuccess()
            showLaunchMainPage()
            return
        }
        //进入登录页面
        showLaunchLoginPage()
    }

    /**
     * 进入权限页面
     */
    private fun showLaunchPermissionPage(){
        onIntent(PermissionsAct::class.java)
        finish()
    }

    /**
     * 进入主页
     */
    private fun showLaunchMainPage(){
        onIntent(NittyTabAct::class.java)
        finish()
    }

    /**
     * 进入登录页
     */
    private fun showLaunchLoginPage() {
        onIntent(LoginAct::class.java)
        finish()
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(NittyEventPointType.PAGE.NITTY_PAGE_GUIDE_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        GuideFactory.clear()
        addEventPointRequestAction(NittyEventPointType.PAGE.NITTY_PAGE_GUIDE_END)
    }
}